package com.edu.exam.enums.assessment.technical;

import com.edu.exam.enums.core.BaseEnum;

/**
 * 技术风险级别枚举
 *
 * <p>定义评估系统中的技术风险级别，用于评估题库质量、技术操作等风险等级。</p>
 *
 * @author System
 * @version 1.0.0
 * @since 2025-11-17
 */
public enum RiskLevel implements BaseEnum {

    /**
     * 无风险
     */
    NONE("NONE", "无风险"),

    /**
     * 低风险
     */
    LOW("LOW", "低风险"),

    /**
     * 中低风险
     */
    LOW_MEDIUM("LOW_MEDIUM", "中低风险"),

    /**
     * 中等风险
     */
    MEDIUM("MEDIUM", "中等风险"),

    /**
     * 中高风险
     */
    MEDIUM_HIGH("MEDIUM_HIGH", "中高风险"),

    /**
     * 高风险
     */
    HIGH("HIGH", "高风险"),

    /**
     * 很高风险
     */
    VERY_HIGH("VERY_HIGH", "很高风险"),

    /**
     * 极高风险
     */
    EXTREME("EXTREME", "极高风险"),

    /**
     * 关键风险
     */
    CRITICAL("CRITICAL", "关键风险"),

    /**
     * 致命风险
     */
    FATAL("FATAL", "致命风险"),

    /**
     * 未知风险
     */
    UNKNOWN("UNKNOWN", "未知风险"),

    /**
     * 待评估
     */
    PENDING("PENDING", "待评估"),

    /**
     * 已评估
     */
    ASSESSED("ASSESSED", "已评估"),

    /**
     * 已接受
     */
    ACCEPTED("ACCEPTED", "已接受"),

    /**
     * 已缓解
     */
    MITIGATED("MITIGATED", "已缓解"),

    /**
     * 已转移
     */
    TRANSFERRED("TRANSFERRED", "已转移"),

    /**
     * 已避免
     */
    AVOIDED("AVOIDED", "已避免"),

    /**
     * 已监控
     */
    MONITORED("MONITORED", "已监控"),

    /**
     * 可控风险
     */
    CONTROLLABLE("CONTROLLABLE", "可控风险"),

    /**
     * 不可控风险
     */
    UNCONTROLLABLE("UNCONTROLLABLE", "不可控风险"),

    /**
     * 可接受风险
     */
    ACCEPTABLE("ACCEPTABLE", "可接受风险"),

    /**
     * 不可接受风险
     */
    UNACCEPTABLE("UNACCEPTABLE", "不可接受风险"),

    /**
     * 可预测风险
     */
    PREDICTABLE("PREDICTABLE", "可预测风险"),

    /**
     * 不可预测风险
     */
    UNPREDICTABLE("UNPREDICTABLE", "不可预测风险"),

    /**
     * 可量化风险
     */
    QUANTIFIABLE("QUANTIFIABLE", "可量化风险"),

    /**
     * 不可量化风险
     */
    UNQUANTIFIABLE("UNQUANTIFIABLE", "不可量化风险"),

    /**
     * 残余风险
     */
    RESIDUAL("RESIDUAL", "残余风险"),

    /**
     * 固有风险
     */
    INHERENT("INHERENT", "固有风险");

    private final String code;
    private final String description;

    RiskLevel(String code, String description) {
        this.code = code;
        this.description = description;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getDescription() {
        return description;
    }

    /**
     * 获取风险等级数值（用于排序和比较）
     *
     * @return 风险等级数值，值越大风险越高
     */
    public int getLevelValue() {
        switch (this) {
            case NONE: return 0;
            case LOW: return 1;
            case LOW_MEDIUM: return 2;
            case MEDIUM: return 3;
            case MEDIUM_HIGH: return 4;
            case HIGH: return 5;
            case VERY_HIGH: return 6;
            case EXTREME: return 7;
            case CRITICAL: return 8;
            case FATAL: return 9;
            case UNKNOWN: return -1;
            case PENDING: return -2;
            default: return 0;
        }
    }

    /**
     * 判断是否为高风险
     *
     * @return 是否为高风险及以上级别
     */
    public boolean isHighRisk() {
        return this == HIGH || this == VERY_HIGH || this == EXTREME || 
               this == CRITICAL || this == FATAL;
    }

    /**
     * 判断是否为低风险
     *
     * @return 是否为低风险及以下级别
     */
    public boolean isLowRisk() {
        return this == NONE || this == LOW;
    }

    /**
     * 判断是否为可接受风险
     *
     * @return 是否为可接受的风险级别
     */
    public boolean isAcceptable() {
        return this == NONE || this == LOW || this == ACCEPTED || 
               this == MITIGATED || this == TRANSFERRED || this == AVOIDED ||
               this == ACCEPTABLE || this == MONITORED;
    }

    /**
     * 根据编码获取对应的枚举
     *
     * @param code 编码
     * @return 对应的枚举
     */
    public static RiskLevel fromCode(String code) {
        for (RiskLevel level : values()) {
            if (level.getCode().equals(code)) {
                return level;
            }
        }
        throw new IllegalArgumentException("Unknown risk level code: " + code);
    }

    /**
     * 根据风险等级数值获取对应的枚举
     *
     * @param levelValue 风险等级数值
     * @return 对应的枚举
     */
    public static RiskLevel fromLevelValue(int levelValue) {
        for (RiskLevel level : values()) {
            if (level.getLevelValue() == levelValue) {
                return level;
            }
        }
        return UNKNOWN;
    }

    @Override
    public String toString() {
        return this.code + "(" + this.description + ")";
    }
}
